// pages/location/location.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    popBoole: true,
    innerShowData: null, //用户隐私保护指引授权状态
  },
  openSetting() {
    const that = this;
    that.setData({
      popBoole: false,
    })
    //检查是否用户隐私保护指引授权
    if (wx.getPrivacySetting) {
      wx.getPrivacySetting({
        success: res => {
          console.log("是否需要授权：", res.needAuthorization, "隐私协议的名称为：", res.privacyContractName)
          if (res.needAuthorization) {
            that.setData({
              innerShowData: true
            })
          } else {
            that.setData({
              innerShowData: false
            })
            let getLocationBool = wx.getStorageSync('locationBool');
            console.log(getLocationBool, 8888)
            if (getLocationBool) {
              wx.openSetting({
                success(res) {
                  wx.setStorageSync("locationBool", true);
                  app.globalData.locationBool = true
                  app.globalData.locationAuthorization = res.authSetting['scope.userLocation'];
                  if (res.authSetting['scope.userLocation']) {
                    app.utils.getLocation().then(res => {
                      app.globalData.locationAuthorization = true;
                      console.log(res);
                      app.globalData.location = res;
                      let json = {
                        lat: res.latitude,
                        lng: res.longitude,
                        coordinate: "GCJ02",
                      }
                      let jsonData = app.utils.getSignData(json);
                      app.utils.post(app.api.reqUrls.getLocationInfo, jsonData).then(res => {
                        if (res.status == 1) {
                          app.globalData.address = res.result;
                          app.globalData.address.select_store_id = app.globalData.nextdayStore != 0 ? app.globalData.nextdayStore : res.result.select_store_id;
                          app.globalData.nextdayStore = res.result.crd_store_id;
                          app.globalData.isjrdStore = true;
                          app.globalData.tooFar = false;
                          if (app.userAddressReadyCallback) {
                            app.userAddressReadyCallback(res)
                          }
                        } else {
                          throw res.msg
                        }
                      }).catch(err => {
                        app.showInfo(err.toString())
                      })
                    })
                    wx.switchTab({
                      url: '/page/tabBar/index/index?location=true',
                    })
                  }
                }
              })
            } else {
              that.setData({
                popBoole: false,
              })
              app.utils.getLocation().then(res => {
                wx.setStorageSync("locationBool", true);
                app.globalData.locationBool = true
                app.globalData.locationAuthorization = true;
                app.globalData.location = res;
                let json = {
                  lat: res.latitude,
                  lng: res.longitude,
                  coordinate: "GCJ02",
                }
                // console.log(json)
                let jsonData = app.utils.getSignData(json);
                app.utils.post(app.api.reqUrls.getLocationInfo, jsonData).then(res => {
                  if (res.status == 1) {
                    app.globalData.address = res.result;
                    app.globalData.address.select_store_id = app.globalData.nextdayStore != 0 ? app.globalData.nextdayStore : res.result.select_store_id;
                    app.globalData.nextdayStore = res.result.crd_store_id;
                    app.globalData.isjrdStore = true;
                    app.globalData.tooFar = false;
                    if (app.userAddressReadyCallback) {
                      app.userAddressReadyCallback(res)
                    }
                    wx.switchTab({
                      url: '/page/tabBar/index/index?location=true',
                    })
                  } else {
                    throw res.msg
                  }
                })

                console.log("app.js定位经纬度：", res);
              }).catch(err => {
                wx.setStorageSync("locationBool", true);
                app.globalData.locationBool = true;
                console.log(9999)
              })
            }
          }
        },
        fail: () => {},
        complete: () => {},
      })
    } else {
      that.setData({
        popBoole: false,
      })
      let getLocationBool = wx.getStorageSync('locationBool');
      console.log(getLocationBool, 8888)
      if (getLocationBool) {
        wx.openSetting({
          success(res) {
            wx.setStorageSync("locationBool", true);
            app.globalData.locationBool = true
            app.globalData.locationAuthorization = res.authSetting['scope.userLocation'];
            if (res.authSetting['scope.userLocation']) {
              app.utils.getLocation().then(res => {
                app.globalData.locationAuthorization = true;
                console.log(res);
                app.globalData.location = res;
                let json = {
                  lat: res.latitude,
                  lng: res.longitude,
                  coordinate: "GCJ02",
                }
                let jsonData = app.utils.getSignData(json);
                app.utils.post(app.api.reqUrls.getLocationInfo, jsonData).then(res => {
                  if (res.status == 1) {
                    app.globalData.address = res.result;
                    app.globalData.address.select_store_id = app.globalData.nextdayStore != 0 ? app.globalData.nextdayStore : res.result.select_store_id;
                    app.globalData.nextdayStore = res.result.crd_store_id;
                    app.globalData.isjrdStore = true;
                    app.globalData.tooFar = false;
                    if (app.userAddressReadyCallback) {
                      app.userAddressReadyCallback(res)
                    }
                  } else {
                    throw res.msg
                  }
                }).catch(err => {
                  app.showInfo(err.toString())
                })
              })
              wx.switchTab({
                url: '/page/tabBar/index/index?location=true',
              })
            }
          }
        })
      } else {
        app.utils.getLocation().then(res => {
          wx.setStorageSync("locationBool", true);
          app.globalData.locationBool = true
          app.globalData.locationAuthorization = true;
          app.globalData.location = res;
          let json = {
            lat: res.latitude,
            lng: res.longitude,
            coordinate: "GCJ02",
          }
          // console.log(json)
          let jsonData = app.utils.getSignData(json);
          app.utils.post(app.api.reqUrls.getLocationInfo, jsonData).then(res => {
            if (res.status == 1) {
              app.globalData.address = res.result;
              app.globalData.address.select_store_id = app.globalData.nextdayStore != 0 ? app.globalData.nextdayStore : res.result.select_store_id;
              app.globalData.nextdayStore = res.result.crd_store_id;
              app.globalData.isjrdStore = true;
              app.globalData.tooFar = false;
              if (app.userAddressReadyCallback) {
                app.userAddressReadyCallback(res)
              }
              wx.switchTab({
                url: '/page/tabBar/index/index?location=true',
              })
            } else {
              throw res.msg
            }
          })

          console.log("app.js定位经纬度：", res);
        }).catch(err => {
          wx.setStorageSync("locationBool", true);
          app.globalData.locationBool = true;
          console.log(err,9999)
        })
      }
    }

  },
  remPop() {
    const that = this;
    that.setData({
      popBoole: false
    })
  }
})